﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LibrarySync2.Business.Extensions
{
    public static class DirectoryInfoExtensions
    {
        public static void CreateSubdirectories(this DirectoryInfo toCreate, string path)
        {
            var dirInfo = new DirectoryInfo(toCreate.FullName);
            var pathBits = path.Split(Path.DirectorySeparatorChar);
            foreach (var bit in pathBits)
            {
                if (string.IsNullOrEmpty(bit))
                    continue;

                var updatedPath = string.Concat(dirInfo.FullName, Path.DirectorySeparatorChar.ToString(), bit);
                var existingDirInfo = new DirectoryInfo(updatedPath);
                if (existingDirInfo.Exists)
                {
                    dirInfo = new DirectoryInfo(updatedPath);
                    continue;
                }    

                dirInfo.CreateSubdirectory(bit);
                dirInfo = new DirectoryInfo(updatedPath);
            }
        }
    }
}
